package com.jf.blog.controller;

import com.jf.blog.bean.Userinfo;
import com.jf.blog.form.userinfo.UserinfoExamForm;
import com.jf.blog.form.userinfo.UserinfoPasswordForm;
import com.jf.blog.form.userinfo.UserinfoSearchForm;
import com.jf.blog.form.userinfo.UserinfoUpdateForm;
import com.jf.blog.service.UserinfoService;
import com.jf.blog.util.MyConstants;
import com.jf.blog.util.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author JF
 * @since 2025-06-20
 */
@RestController
@RequestMapping("/userinfo")
@Api(tags = "用户模块")
public class UserinfoController {
    @Autowired
    private UserinfoService userinfoService;

    @PostMapping("/update")
    @ApiOperation("添加或修改")
    public ResultJson update(@RequestBody UserinfoUpdateForm form) {
        userinfoService.saveOrUpdate(form);
        return ResultJson.success();
    }


    @PostMapping("/exam")
    @ApiOperation("会员审核")
    public ResultJson exam(@RequestBody UserinfoExamForm form) {
        userinfoService.exam(form);
        return ResultJson.success();
    }

    @PostMapping("/password")
    @ApiOperation("修改密码")
    public ResultJson password(@RequestBody UserinfoPasswordForm form) {
        userinfoService.password(form);
        return ResultJson.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页筛选")
    public ResultJson page(@RequestParam(defaultValue = "1") @ApiParam(required = true) int page, @RequestParam(defaultValue = "5") @ApiParam(required = true) int limit, UserinfoSearchForm form) {
        Userinfo userinfo = new Userinfo();
        BeanUtils.copyProperties(form, userinfo);
        userinfo.setRoleId(MyConstants.ROLE_ADMIN);
        return userinfoService.getPage(page, limit, userinfo);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户")
    public ResultJson delete(@RequestBody List<Integer> userinfoIds) {
        userinfoService.delete(userinfoIds);
        return ResultJson.success();
    }

    @GetMapping("/info/{userinfoId}")
    @ApiOperation("用户详情")
    public ResultJson info(@PathVariable int userinfoId) {
        return ResultJson.success(userinfoService.getById(userinfoId));
    }

    @PostMapping("/status/{userinfoId}")
    @ApiOperation("修改状态")
    public ResultJson status(@PathVariable int userinfoId) {
        userinfoService.updateStatus(userinfoId);
        return ResultJson.success();
    }

    @GetMapping("/select")
    @ApiOperation("下拉框")
    public ResultJson select() {
        return userinfoService.select();
    }

}
